package com.ruoyi.imweb.test.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.imweb.test.entity.test.RoomSearchTest;
import com.ruoyi.imweb.test.entity.test.RoomTest;
import com.ruoyi.imweb.test.entity.test.UserTest;
import org.apache.commons.compress.utils.Lists;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user/test")
public class UserTestController {
    private Map<String, List<RoomTest>> rooms = new HashMap<>();
    private Map<String, String> idToNameMap = new HashMap<>();

    private Map<String, JSONObject> roomUsers = new HashMap<>();

    @PostConstruct
    public void init() {
        rooms.put("Leia",JSONArray.parseArray(CollectionUtil.join(FileUtil.readLines(ResourceUtil.getResource("Leia_rooms.json"),"UTF-8"),""),RoomTest.class));
        rooms.put("Luke",JSONArray.parseArray(CollectionUtil.join(FileUtil.readLines(ResourceUtil.getResource("Luke_rooms.json"),"UTF-8"),""),RoomTest.class));
        rooms.put("Yoda", JSONArray.parseArray(CollectionUtil.join(FileUtil.readLines(ResourceUtil.getResource("Yoda_rooms.json"), "UTF-8"), ""), RoomTest.class));

        idToNameMap.put("6R0MijpK6M4AIrwaaCY2", "Luke");
        idToNameMap.put("SGmFnBZB4xxMv9V4CVlW", "Leia");
        idToNameMap.put("6jMsIXUrBHBj7o2cRlau", "Yoda");

        JSONArray users = JSONArray.parseArray(CollectionUtil.join(FileUtil.readLines(ResourceUtil.getResource("users.json"), "UTF-8"), ""));
        for (Object user : users) {
            JSONObject userTemp = (JSONObject) user;
            roomUsers.put(userTemp.getString("id"), userTemp);
        }

    }
    @GetMapping()
    public AjaxResult getInfo() {

        List<UserTest> result = new ArrayList<>();
        result.add(UserTest.builder().id("6R0MijpK6M4AIrwaaCY2").username("Luke").avatar("https://66.media.tumblr.com/avatar_c6a8eae4303e_512.pnj").build());
        result.add(UserTest.builder().id("SGmFnBZB4xxMv9V4CVlW").username("Leia").avatar("https://media.glamour.com/photos/5695e9d716d0dc3747eea3ef/master/w_1600,c_limit/beauty-2015-12-princess-leia-1-main.jpg").build());
        result.add(UserTest.builder().id("6jMsIXUrBHBj7o2cRlau").username("Yoda").avatar("https://vignette.wikia.nocookie.net/teamavatarone/images/4/45/Yoda.jpg/revision/latest?cb=20130224160049").build());
        return AjaxResult.success(result);
    }

    /**
     * 获取用户详情。只是为了获取房间名称
     * @param roomSearchTest
     * @return
     */
    @GetMapping("/user_details")
    public AjaxResult getUserDetails(RoomSearchTest roomSearchTest) {
        if (CollectionUtil.isEmpty(roomSearchTest.getUserIds())) {
            return AjaxResult.success(Lists.newArrayList());
        }
        return AjaxResult.success(roomSearchTest.getUserIds().stream().map(userId -> roomUsers.get(userId)).collect(Collectors.toList()));
    }

    /**
     * 查询房间
     * @param roomSearchTest
     * @return
     */
    @GetMapping("/rooms")
    public AjaxResult getRooms(RoomSearchTest roomSearchTest) {
        String name = idToNameMap.get(roomSearchTest.getCurrentUserId());
        if (StringUtils.isEmpty(name)) {
            return AjaxResult.success(Lists.newArrayList());
        }
        if (roomSearchTest.getRoomsPerPage() == null) {
            roomSearchTest.setRoomsPerPage(15);
        }
        List<RoomTest> roomTests = rooms.get(name);
        if (CollectionUtil.isEmpty(roomTests)) {
            return AjaxResult.success(Lists.newArrayList());
        }
        int start=0;
        // 为空表示第一页
        if (roomSearchTest.getStartRooms() != null) {
            for (int i = 0; i < roomTests.size(); i++) {
                if (roomTests.get(i).getId().equals(roomSearchTest.getStartRooms())) {
                    start = i+1;
                    break;
                }
            }
        }
        return AjaxResult.success(CollectionUtil.sub(roomTests, start, start+roomSearchTest.getRoomsPerPage()));
    }
}
